home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / mus / edit / AlgoRhythms.lha / AlgoRhythms / Source / musictimer.c < prev    next >
C/C++ Source or Header  |  1994-11-25  |  5KB  |  213 lines

  1. /*MusicTimer.c
  2.     Copyright (c) 1990,1991,1992,1993 by Thomas E. Janzen
  3.     All Rights Reserved
  4.  
  5.     THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
  6.     BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
  7.     CHANGES FOR FREE DISTRIBUTION.  NO TITLE TO AND OWNERSHIP OF THE
  8.     SOFTWARE IS HEREBY TRANSFERRED.  THOMAS E. JANZEN ASSUMES NO 
  9.     RESPONSIBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
  10.     
  11.     Thomas E. Janzen
  12.     208A Olde Derby Road
  13.     Norwood, MA  02062-1761
  14.     (617)769-7733
  15.  
  16. **  FACILITY:
  17. **
  18. **    AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
  19. **    compiled with SAS/C Amiga Compiler 6.50 
  20. **
  21. **  ABSTRACT:
  22. **
  23. **    MusicTimer.c manages the timer device.
  24. **    Time is kept in a double floating-point number in seconds.
  25. **    Notes starting times are noted and when their length has passed
  26. **    they are stopped.
  27. **
  28. **  AUTHORS: Thomas E. Janzen
  29. **
  30. **  CREATION DATE:    26-MAR-1990
  31. **
  32. **  MODIFICATION HISTORY:
  33. **    DATE    NAME    DESCRIPTION
  34. **    8 DEC 91 T. Janzen conform to SAS/C 5.10b remove extern from functs
  35. **  4 Jan 92 TEJ  last changes for 2.0
  36. **--
  37. */
  38.  
  39. #include <exec/types.h>
  40. #include <exec/nodes.h>
  41. #include <exec/lists.h>
  42. #include <exec/memory.h>
  43. #include <exec/interrupts.h>
  44. #include <exec/ports.h>
  45. #include <exec/libraries.h>
  46. #include <exec/tasks.h>
  47. #include <exec/io.h>
  48. #include <exec/devices.h>
  49. #include <devices/timer.h>
  50. #include <proto/dos.h>
  51. #include <proto/graphics.h>
  52. #include <proto/exec.h>
  53. #include <proto/mathffp.h>
  54. #include <proto/intuition.h>
  55. #include "Window.h"
  56. #include "MusicTimer.h"
  57.  
  58. struct Library *TimerBase;
  59. static struct timerequest *tr;
  60.  
  61. extern struct IORequest *CreateExtIO();
  62. extern struct timerequest *CreateTimer(ULONG unit);
  63. static void delete_timer(struct timerequest *);
  64.  
  65. struct timerequest *CreateTimer(ULONG unit)
  66. /*
  67. ** FUNCTIONAL DESCRIPTION:
  68. **  Connects this process to a timer device
  69. **
  70. ** RETURN VALUE:
  71. **      description: 
  72. **        data_type: pointer to timerequest
  73. **
  74. ** ARGUMENTS:
  75. **
  76. **  unit-
  77. **         description: 
  78. **           data_type: ULONG
  79. **              access: read only
  80. **
  81. ** DESIGN:
  82. **  ROUTINE
  83. **  : timerport = CreatePort()
  84. **  : IF failed
  85. **  : : return NULL
  86. **  : ENDIF
  87. **  : timermsg = CreateExtIO()
  88. **  : IF failed
  89. **  : : return NULL
  90. **  : ENDIF
  91. **  : OpenDevice()
  92. **  : IF failed
  93. **  : : delete_timer()
  94. **  : : return NULL
  95. **  : ENDIF
  96. **  : return timermsg
  97. **  ENDROUTINE
  98. */
  99. {
  100.     auto int error;
  101.     auto struct MsgPort *timerport;
  102.     auto struct timerequest *timermsg;
  103.  
  104.     timerport = CreatePort(NULL, 0);
  105.     if (NULL == timerport) 
  106.     {
  107.         return NULL;
  108.     }
  109.     timermsg = (struct timerequest *)
  110.         CreateExtIO(timerport, sizeof(struct timerequest));
  111.     if (NULL == timermsg)
  112.     {
  113.         return NULL;
  114.     }
  115.     error = OpenDevice(TIMERNAME, unit, (struct IORequest *)timermsg, 0L);
  116.     if (error != 0)
  117.     {
  118.         delete_timer(timermsg);
  119.         return NULL;
  120.     }
  121.     return timermsg;
  122. }
  123.  
  124. int start_timer(void)
  125. /*
  126. ** FUNCTIONAL DESCRIPTION:
  127. **  Begin using the timer device
  128. **
  129. ** RETURN VALUE:
  130. **      description: 
  131. **        data_type: int
  132. **
  133. ** DESIGN:
  134. **  ROUTINE
  135. **  : tr = CreateTimer(UNIT_MICROHZ)
  136. **  : IF failed
  137. **  : : return -1
  138. **  : ENDIF
  139. **  : TimerBase = tr->tr_node.io_Device
  140. **  : return 0
  141. **  ENDROUTINE
  142. */
  143. {
  144.     /* 
  145.     ** start the timer at the beginning of the program
  146.     */
  147.     tr = CreateTimer(UNIT_MICROHZ);
  148.     if (NULL == tr) 
  149.     {
  150.         return -1;
  151.     }
  152.     TimerBase = (struct Library *)tr->tr_node.io_Device;
  153.     return 0;
  154. }
  155.  
  156. void remove_timer(void) 
  157. /*
  158. ** FUNCTIONAL DESCRIPTION:
  159. **  Delete the timer device connection
  160. */
  161. {
  162.     /* 
  163.     ** delete the timer at end of program 
  164.     */
  165.     delete_timer(tr);
  166.     return;
  167. }
  168.  
  169. static void delete_timer(struct timerequest *tr)
  170. /*
  171. ** FUNCTIONAL DESCRIPTION:
  172. **  Really delete a connection to the timer device
  173. **
  174. ** ARGUMENTS:
  175. **
  176. **  tr-
  177. **         description: 
  178. **           data_type: pointer to timerequest
  179. **              access: read/write only
  180. **
  181. ** DESIGN:
  182. **  ROUTINE
  183. **  : IF tr is valid
  184. **  : : get timer port
  185. **  : : IF timer port is valid
  186. **  : : : DeletePort(tp)
  187. **  : : ENDIF
  188. **  : : IF tr is valid
  189. **  : : : CloseDevice(tr)
  190. **  : : ENDIF
  191. **  : : DeleteExtIO(tr)
  192. **  : ENDIF
  193. **  ENDROUTINE
  194. */
  195. {
  196.     auto struct MsgPort *tp;
  197.  
  198.     if (tr != NULL)
  199.     {
  200.         tp = tr->tr_node.io_Message.mn_ReplyPort;
  201.         if (tp != NULL)
  202.         {
  203.             DeletePort(tp);
  204.         }
  205.         if (tr != NULL) 
  206.         {
  207.             CloseDevice((struct IORequest *)tr);
  208.         }
  209.         DeleteExtIO((struct IORequest *)tr, sizeof(struct timerequest));
  210.     }
  211.     return;
  212. }
  213.